//==============================================================================
//
//               Copyright (C) 2002  Dick van Oudheusden
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2003-10-31 08:12:34 $ $Revision: 1.1 $
//
//==============================================================================

#include <stdlib.h>

#include "ofc/config.h"

#include "ofc/DText.h"
#include "ofc/DInt.h"
#include "ofc/DHashTable.h"

#include "DInc.h"
#include "DTest.h"

//-Collections-----------------------------------------------------------------

void DHashTable_test(void)
{
  DHashTable *map = [DHashTable alloc];
  DText      *key;
  DText      *val;
  int         i;
  
  [map init :[DText class]];
  
  STARTTEST();

  TEST([map isEmpty]);

  key = [DText alloc]; [key init :"hello"];
  val = [DText alloc]; [val init :"test"];
  TEST([map insert :key :val]);

  key = [DText alloc]; [key init :"bye"];
  val = [DText alloc]; [val init :"test"];
  TEST([map insert :key :val]);

  TEST([map length] == 2);

  key = [DText alloc]; [key init :"see you"];
  TEST(![map has :key]);
  TEST([map delete   :key] == nil);
  
  [key set :"bye"];
  TEST([map has :key]);
  TEST([map delete   :key] != nil);
  TEST(![map has :key]);

  [map size :301];
  [key set :"hello"];
  TEST([map has :key]);

  TEST([map size] == 301 && [map length] == 1);

  [map each :@selector(upper)];

  val = [map get :key];
  TEST(val != nil);
  TEST([val ccompare :"TEST"] == 0);

  [map free];

  map = [DHashTable alloc];

  [map init :[DInt class] :1001 :2.0];

  for (i = 0; i < 10000; i++)
  {
    DInt *key = [DInt alloc];
    DInt *val;

    [key set :i];

    val = [key copy];

    [map insert :key :val];
  }

  TEST([map length] == 10000 && [map size] == 8015);

  {
    DInt *key = [DInt alloc];

    [key set :0];
    TEST([map has :key]);
    [key set :1234];
    TEST([map has :key]);
    [key set :9999];
    TEST([map has :key]);
    [key set :10000];
    TEST(![map has :key]);

    [key free];
  }

  STOPTEST();
} 

